<script setup>
import { reactive, ref, toRaw } from 'vue';
import { REGISTER, FIND_PASSWORD, HOME } from '@/router/routeMap'
import router from '@/router';

defineProps({
  isLoading: Boolean
})
const emit = defineEmits(['clickLogin'])

const title = ref('欢迎登陆')
const formData = reactive({
  username: '',
  password: '',
})
const formRef = ref()
const rules = reactive({
  username: { required: true, type: 'string', trigger: 'blur' },
  password: { required: true, type: 'string', trigger: 'blur' }
})

const onClickLogin = () => {
  if (!formRef.value) return
  formRef.value.validate((valid) => {
    if (valid) {
      emit('clickLogin', { ...toRaw(formData) })
    } else {
      return false
    }
  })
}

const onClickEnter = () => {
  router.push(HOME)
}
</script>

<template>
  <el-form
    class="form-container"
    label-width="100px"
    :model="formData"
    :rules="rules"
    ref="formRef"
  >
    <h1 class="caption">{{ title }}</h1>
    <el-form-item label="用户名：" prop="username">
      <el-input v-model="formData.username" />
    </el-form-item>
    <el-form-item label="密码：" prop="password">
      <el-input type="password" v-model="formData.password" />
    </el-form-item>
    <el-form-item>
      <el-button class="button" type="primary" @click="onClickLogin" :loading="isLoading">登录</el-button>
    </el-form-item>
    <el-form-item>
      <el-button class="button" @click="onClickEnter" :loading="isLoading">暂不登录，进去逛逛</el-button>
    </el-form-item>
    <el-form-item>
      <el-row justify="space-between" class="path-link">
        <router-link :to="REGISTER">立即注册</router-link>
        <!-- <router-link :to="FIND_PASSWORD">忘记密码</router-link> -->
      </el-row>
    </el-form-item>
  </el-form>
</template>

<style lang="sass" scoped>

.caption
  margin-bottom: 30px
  text-align: center

.form-container
  width: 30vw
  min-width: 400px
  margin: auto
  margin-top: 10vh
  .button
    width: 100%

.path-link
  width: 100%
  a
    font-size: 12px
    text-decoration: none
</style>